package com.heima.wemedia.client;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class WemediaClient implements IWemediaClient {

    private final WmUserService wmUserService;

    private final WmChannelService wmChannelService;
    /**
     * 根据名称查询自媒体用户
     * @param name
     * @return
     */
    @GetMapping("/api/v1/user/findByName/{name}")
    public WmUser findWmUserByName(@PathVariable("name") String name){
        return wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));

    }

    /**
     * 保存自媒体用户
     *
     * @param wmUser
     */
    @GetMapping("/api/v1/wm_user/save")
    public void saveWmUser(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询所有频道
     * @return
     */
    @Override
    @GetMapping("/api/v1/channel/list")
    public ResponseResult getChannels() {
        return ResponseResult.okResult(wmChannelService.findAll());
    }
}
